// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <gtest/gtest.h>

#include <pollux/experimental/wave/common/StringView.h>

namespace kumo::pollux::wave {
namespace {

TEST(StringViewTest, empty) {
  StringView sv;
  sv.init(reinterpret_cast<const char*>(0xDEADBEEFBADEFEEDll), 0);
  ASSERT_EQ(sv.size(), 0);
  ASSERT_EQ(sv, StringView{});
}

TEST(StringViewTest, inlined) {
  StringView sv;
  sv.init("foobarquux", 3);
  ASSERT_EQ(sv.size(), 3);
  ASSERT_EQ(strncmp(sv.data(), "foo", 3), 0);
  ASSERT_LT(
      reinterpret_cast<uintptr_t>(&sv), reinterpret_cast<uintptr_t>(sv.data()));
  ASSERT_LT(
      reinterpret_cast<uintptr_t>(sv.data()),
      reinterpret_cast<uintptr_t>(&sv + 1));
  ASSERT_NE(sv, StringView{});
  StringView sv2;
  sv2.init("foobar", 3);
  ASSERT_EQ(sv, sv2);
  sv2.init("foobar", 4);
  ASSERT_NE(sv, sv2);
  sv2.init("quux", 3);
  ASSERT_NE(sv, sv2);
}

TEST(StringViewTest, nonInlined) {
  StringView sv;
  const char* data = "foobarquux";
  sv.init(data, 10);
  ASSERT_EQ(sv.size(), 10);
  ASSERT_EQ(strncmp(sv.data(), data, 10), 0);
  ASSERT_EQ(sv.data(), data);
  const char* data2 = "foobarquux2";
  StringView sv2;
  sv2.init(data2, 10);
  ASSERT_EQ(sv, sv2);
  sv2.init(data2, 11);
  ASSERT_NE(sv, sv2);
  sv2.init(data, 3);
  ASSERT_NE(sv, sv2);
}

} // namespace
} // namespace kumo::pollux::wave
